package cn.argentoaskia.crm.controller;


import cn.argentoaskia.crm.result.ResponseData;
import cn.argentoaskia.crm.service.login.HrService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

// 方法1：spring写法
//@Controller
//@ResponseBody
// 方法2：springboot写法
@RestController
@RequestMapping("/users")
public class HrController {

    @Autowired
    private HrService hrService;

    // @RequestMapping指定路径
    // @GetMapping、@PostMapping
    // 方法1：@RequestMapping(method = RequestMethod.POST, value = "/login")
    @PostMapping("/login")
    public ResponseData loginController(@RequestBody String userLoginJson){
        // 2022/9/15  1. 把userLoginJson转为一个对象，fastjson，有专门的处理对象
        // JSONObject类,
        // parseXX():把json字符串转为对象
        // toJsonString():toXXX()，把对象转为json字符串

        // JSONObject:是一个Map，有get、put方法
        JSONObject jsonObject = JSONObject.parseObject(userLoginJson);

        // 2.调用业务层代码，处理登录逻辑
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        // 0x12345 --> 0x12345 0x1235
        // 0x12345-0x12349 = code
        // 0x12349-0x12362 = info
        ResponseData responseData = new ResponseData();
        boolean login = hrService.login(username, password, responseData);

        // 3. 判断业务结果，设置相应的响应状态
        if (login){
            responseData.setCode(200);
            responseData.setInfo("success");
        }else{
            responseData.setCode(501);
            responseData.setInfo("数据异常");
        }
        return responseData;
    }
}
